/*
Copyright 2008, Mark Arvidson

This file is part of EwaScorecard.

EwaScorecard is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

EwaScorecard is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with EwaScorecard.  If not, see <http://www.gnu.org/licenses/>.
 */

import javax.microedition.lcdui.*;

public class EnterScoreForm extends Form
        implements CommandListener, ItemStateListener {

    // Gymnast info form
    private ChoiceGroup gymnast;
    private ChoiceGroup teamChoice;
    private ChoiceGroup eventGroup;
    private TextField score = new TextField("Score:", null, 6, TextField.DECIMAL);
    private Command backCommand = new Command("Cancel", Command.BACK, 0);
    private Command okCommand = new Command("OK", Command.OK, 0);
    private MeetData meetData;
    private TeamData teamData;
    private EwaScorecard host;

    public EnterScoreForm(EwaScorecard host, MeetData meetData) {
        super("Gymnast Info");
        this.meetData = meetData;
        this.host = host;
        append(score);
        teamChoice = new ChoiceGroup("Team", Choice.POPUP, meetData.getTeams(), null);
        append(teamChoice);
        gymnast = new ChoiceGroup("Gymnast", Choice.POPUP, new String[]{}, null);
        append(gymnast);
        eventGroup = new ChoiceGroup("Event", Choice.EXCLUSIVE, meetData.getEvents(), null);
        eventGroup.delete(eventGroup.size() - 1);
        append(eventGroup);
        addCommand(backCommand);
        addCommand(okCommand);
        setCommandListener(this);
        setItemStateListener(this);
    }

    public void clearScore() {
        score.setString(null);
    }

    public void edit(int row, int col, int activeTeamIndex) {
        teamChoice.setSelectedIndex(activeTeamIndex, true);
        updateGymnasts();
        gymnast.setSelectedIndex(row, true);
        eventGroup.setSelectedIndex(col, true);
        float currentScore = teamData.rowData(row, col);
        if (currentScore > 0) {
            score.setString(String.valueOf(currentScore));
        } else {
            score.setString("");
        }
    }

    public void commandAction(Command c, Displayable d) {
        float scoreValue = 0;
        try {
            scoreValue = Float.parseFloat(score.getString());
        } catch (NumberFormatException e) {
        }
        if (c == okCommand) {
            teamData.setScore(gymnast.getSelectedIndex(),
                    eventGroup.getSelectedIndex(),
                    scoreValue);
            SaveMeetDataForm.saveMeet(host, meetData);
            host.viewScores();
        } else if (c == backCommand) {
            host.viewScores();
        }
    }

    public void itemStateChanged(Item item) {
        if (item == teamChoice) {
            updateGymnasts();
        }
    }

    private void updateGymnasts() {
        teamData = meetData.getTeam(teamChoice.getSelectedIndex());
        while (gymnast.size() > 0) {
            gymnast.delete(0);
        }
        String[] gymnasts = teamData.getGymnasts();
        for (int index = 0; index < gymnasts.length; index++) {
            gymnast.append(gymnasts[index], null);
        }
    }
}

